<template>
	<uni-popup ref="popup_select_device" type="bottom" border-radius="16px 16px 0 0" :before-close="true" @maskClick="closePopup">
		<view class="pop-box pad-20">
			<view class="flex flex-between flex-center">
				<view class="col-000-8 font-18 font-600">
					<!-- 选择收藏集 -->
					{{ $t('selectCollect.selectCollect') }}
				</view>
				<view class="close" @click="closePopup">
					<uni-icons type="closeempty" color="rgba(0,0,0,0.4)" size="20"></uni-icons>
				</view>
			</view>
			<!-- 我的收藏集 -->
			<view class="devices-list">
				<view class="choose-all" @click="doCheckAll">
          <view class="check">
            <view class="check-div flex-center-center" :class="{on: checkAll == '1'}">
						  <uni-icons class="ico" type="checkmarkempty" color="#fff" size="16"></uni-icons>
            </view>
            <view class="font-14 col-000-4 font-500">
              <!-- 全选 -->
              {{ $t('selectCollect.chooseAll') }}
            </view>
          </view>
				</view>
				<view class="item" v-for="(item, index) in deviceList" :key="item.id" @click="doCheck(index)">
					<view class="check">
						<view class="check-div flex-center-center" :class="{on: item.check == '1'}">
							<uni-icons class="ico" type="checkmarkempty" color="#fff" size="16"></uni-icons>
						</view>
					</view>
					<view class="">
						<view class="name font-500 font-16 col-000-8">
							{{item.collection_name}}
						</view>
						<!-- <view class="status font-11 col-000-4">
							<span class="dot" :class="{'no': item.status == 0}"></span>
							{{item.collection_status === 1 ? '已收藏' : '未收藏' }}
						</view> -->
					</view>
				</view>
				<view class="btn font-14 font-500 col-fff flex-center-center" @click="doSubmit">
					<!-- 确认 -->
					{{ $t('selectDevice.confirm') }}
				</view>
			</view>
			<view class="empty-box" v-if="deviceList.length == 0">
				<view class="">
					<view class="empty-img">
						<image class="img-100" src="/static/svg/empty.svg" mode="widthFix"></image>
					</view>
					<view class="empty-text">
						{{ $t('list.noDevice') }}
					</view>
				</view>
			</view>
		</view>
	</uni-popup>
</template>

<script>
	import collectionApi from '@/api/collection.js';
	export default {
		emits: ['open', 'success'],
		data() {
			return {
				patternId: '',
        checkAll: '0',
				deviceList: [],
        collectIds: [], //收藏id数组

			}
		},
		created() {
			
		},
		methods: {
			getListApi() {
				collectionApi.getCollectionList().then(res => {
					if(res.errCode === 0) {
						this.deviceList = res.data.list;
            // collectIds 收藏id数组
            this.deviceList.forEach(item => {
              item.check = this.collectIds.includes(item.id) ? '1' : '0'
            })
					}
				})
			},
			doCheck(index) {
				// this.deviceList[index].check == '1' ? this.deviceList[index].check == '1' : this.deviceList[index].check == '0';
				if(this.deviceList[index].check == '1') {
					this.deviceList[index].check = '0'
				}else {
					this.deviceList[index].check = '1'
				}
				this.$forceUpdate()
				console.log(this.deviceList)
			},
      doCheckAll() {
        this.checkAll = this.checkAll == '1' ? '0' : '1'
        this.deviceList.forEach(item => {
          item.check = this.checkAll
        })
        console.log(this.deviceList)
      },
			open(id, collectIds) {
				this.patternId = id;
        this.collectIds = collectIds;
				this.getListApi();
				this.$refs.popup_select_device.open('bottom')
			},
			closePopup() {
				this.patternId = '';
				this.$refs.popup_select_device.close()
				this.$emit('closePopup')
			},
			doSubmit() {
				let ids = this.deviceList.filter(item => item.check == '1').map(item => item.id)
				if(ids.length == 0) {
					uni.showToast({
						title: this.$t('selectCollect.selectCollect'),
						icon: 'none'
					})
					return
				}
				collectionApi.addPatternToCollection({
					collection_id: ids.join(','),
          pattern_id: this.patternId
				}).then(res => {
					if(res.errCode === 0) {
						uni.showToast({
							title: this.$t('selectCollect.confirm'),
							icon: 'none'
						})
						// 触发成功事件，通知父组件更新状态
						this.$emit('success', {
							patternId: this.patternId,
							collectionIds: ids
						})
						this.closePopup()
					}
				}).catch(err => {
					uni.showToast({
						title: err.message,
						icon: 'none'
					})
				})

			}
		}
	}
</script>

<style lang="scss" scoped>
	.pop-box {
		width: 100%;
		background-color: #F3F3F3;
	}

	.devices-list {
		width: 100%;
		margin: 80rpx auto 0;
    max-height: 80vh;
    overflow-y: auto;

		.choose-all {
			width: 100%;
      margin-bottom: 40rpx;
      .check {
        width: 100%;
        display: flex;
        align-items: center;
        justify-content: flex-start;
        .check-div {
          margin-right: 16rpx;
        }
      }
		}
    .check {
				width: 40rpx;
				min-width: 40rpx;
				margin-right: 32rpx;
				.check-div {
					width: 40rpx;
					height: 40rpx;
					border: 2rpx solid rgba(0,0,0,0.4);
					border-radius: 4rpx;
					background-color: #fff;
					.ico {
						display: none;
					}
					&.on {
						background-color: #000;
						.ico {
							display: block;
						}
					}
				}
			}
		.item {
			width: 100%;
			background-color: #fff;
			box-shadow: 0px 0px 8rpx 0px rgba(0, 0, 0, 0.08);
			border-radius: 16rpx;
			padding: 28rpx 32rpx;
			margin-bottom: 16rpx;
			display: flex;
			align-items: center;
			justify-content: flex-start;
			
			.name {
				text-align: left;
			}

			.status {
				text-align: left;
				margin: 8rpx auto 0;

				.dot {
					width: 12rpx;
					height: 12rpx;
					border-radius: 100%;
					margin-right: 8rpx;
					display: inline-block;
					background-color: #22C55D;

					&.no {
						background-color: rgba(0, 0, 0, 0.4);
					}
				}
			}
		}
		.btn {
			width: 670rpx;
			height: 100rpx;
			background-color: #000000;
			border-radius: 16rpx;
			margin: 60rpx auto 0;
		}
	}
	
	.msg-box,
	.add-box {
	  width: 68rpx;
	  height: 68rpx;
	  border-radius: 100%;
	  background-color: #fff;
	  display: flex;
	  align-items: center;
	  justify-content: center;
	
	}
	
	.msg-img {
	  width: 36rpx;
	  height: 44rpx;
	}
	
	.img {
	  width: 40rpx;
	  height: 40rpx;
	}
	
	.add-box {
	  background-color: #3C82F6;
	  margin-left: 32rpx;
		display: flex;
		align-items: center;
		justify-content: center;
	  .add {
			width: 40rpx;
			height: 40rpx;
	    .img {
				
			}
	  }
	}
</style>